package br.com.zynger.mst.addressbook;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

//Refactoring 6: Extract superclass
public class FormFrame extends JFrame {
	private static final long serialVersionUID = 1L;
	
	private JButton btConfirm;
	private JButton btCancel;

	protected void init(String title, int width, int height) {
		setTitle(title);
		setSize(width, height);
		setLocationRelativeTo(null);
		
		JPanel contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		
		setButtonsPane();
	}
	
	private void setButtonsPane(){
		JPanel buttonsPane = new JPanel();
		buttonsPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		buttonsPane.setLayout(new FlowLayout(FlowLayout.CENTER));
		getContentPane().add(buttonsPane, BorderLayout.SOUTH);
		
		btConfirm = new JButton("Confirm");
		btCancel = new JButton("Cancel");
		
		buttonsPane.add(btConfirm);
		buttonsPane.add(btCancel);
	}
	
	public void setConfirmButtonAction(ActionListener actionListener) {
		btConfirm.addActionListener(actionListener);
	}
	
	public void setCancelButtonAction(ActionListener actionListener) {
		btCancel.addActionListener(actionListener);
	}
	
	protected void addFormGrid(JPanel panel, int rows){
		//Lay out the panel.
		SpringUtilities.makeCompactGrid(panel,
				rows, 2, //rows, cols
				6, 6,    //initX, initY
				6, 6);   //xPad, yPad
		
		getContentPane().add(panel, BorderLayout.NORTH);
	}
	
	protected JTextField createFormField(JPanel panel, String label){
		JLabel l = new JLabel(label, JLabel.TRAILING);
		JTextField textField = new JTextField(10);
		l.setLabelFor(textField);
		panel.add(l);
		panel.add(textField);
		return textField;
	}
}
